package com.sky.Aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面类实现公共字段自动填充逻辑
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..))&& @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始公共字段自动填充");
        //获取方法签名上的注解
        //强转为MethodSignature，可用其中的方法调出注解类型
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获取注解对象
        //获取操作类型
        OperationType type = autoFill.value();

        //获取当前被截取到的方法的参数实体对象,对其进行赋值
        Object[] args = joinPoint.getArgs();
        if (args==null||args.length==0){
            return;
        }
        Object enity = args[0];
        //准备赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long userId = BaseContext.getCurrentId();
        if (type==OperationType.INSERT){
            //插入操作，四个字段全部填充
            try{
                Method setUpdateUser = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setCreateTime = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                //通过反射为公共字段赋值
                setUpdateUser.invoke(enity,userId);
                setUpdateTime.invoke(enity,now);
                setCreateUser.invoke(enity,userId);
                setCreateUser.invoke(enity,now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type==OperationType.UPDATE) {
            //更新操作，为两个字段赋值
            try {
                Method setUpdateTime = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = enity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //反射为公共字段赋值
                setUpdateUser.invoke(enity,userId);
                setUpdateTime.invoke(enity,now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
